package bounjour;

import java.sql.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 * @author Senem Beken
 * @date: 07.05.11
 * 
 * This class contains static utility functions that can be called from everywhere
 */
public class Util 
{
	public static boolean validateEmail(String eMail)
	{
		if (eMail == null) return false;
	    boolean result = true;
	    
		try
		{
			InternetAddress validator = new InternetAddress(eMail);
			validator.validate();
		}
		catch(AddressException e)
		{
			System.out.println("E-mail address invalid");
			result = false;
		}
		
		return result;
	}
	
	public static String appendColon(String original)
	{
		if(!original.startsWith("'"))
			original = "'" + original;
		
		if(!original.endsWith("'"))
			original = original + "'";
		
		return original;
	}	
	
	public static Date getCurrentDate()
	{
		java.util.Date registerDate = new java.util.Date();
		java.sql.Date sqlDate = new java.sql.Date(registerDate.getTime());

		return sqlDate;
	}
	
	// for debugging purposes
	public static void printResult(Vector<HashMap<String, Object>> selectedRows)
	{
		for (Enumeration <HashMap<String, Object>> e = selectedRows.elements(); e.hasMoreElements();)
		{
			HashMap <String, Object> resultMap = e.nextElement(); 
			Iterator<Entry<String, Object>>iterator = resultMap.entrySet().iterator();
	        
	        while (iterator.hasNext()) 
	        {
	        	Map.Entry <String, Object>entry = iterator.next();
	            System.out.println( "(" + entry.getKey() + ": " + entry.getValue() + ") " );
	        }
		}	
	}
}
